<template>
  <div class="login-container">
    <el-form
      ref="loginForm"
      :model="loginForm"
      :rules="loginRules"
      class="login-form"
      autocomplete="on"
      label-position="left"
    >
      <div class="title-container">
        <h3 class="title">
          教师登分系统
        </h3>
      </div>

      <el-form-item prop="username" class="loginform-item">
        <span class="svg-container">
          <svg-icon icon-class="user" />
        </span>
        <el-input
          ref="username"
          v-model="loginForm.username"
          class="loginform-input"
          :placeholder="$t('login.username')"
          name="username"
          type="text"
          tabindex="1"
          autocomplete="on"
        />
      </el-form-item>

      <el-tooltip
        v-model="capsTooltip"
        content="Caps lock is On"
        placement="right"
        manual
      >
        <el-form-item prop="password" class="loginform-item">
          <span class="svg-container">
            <svg-icon icon-class="password" />
          </span>
          <el-input
            :key="passwordType"
            ref="password"
            v-model="loginForm.password"
            class="loginform-input"
            :type="passwordType"
            :placeholder="$t('login.password')"
            name="password"
            tabindex="2"
            autocomplete="on"
            @keyup.native="checkCapslock"
            @blur="capsTooltip = false"
            @keyup.enter.native="handleLogin"
          />
          <span class="show-pwd" @click="showPwd">
            <svg-icon
              :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'"
            />
          </span>
        </el-form-item>
      </el-tooltip>

      <!-- 验证码TODO -->
      <el-form-item class="loginform-item" prop="verifycode">
        <span style="color: #889aa4; width: 30px; padding-left: 10px">
          <i class="el-icon-key" style="font-size: 20px; font-weight: bolder;" />
        </span>
        <!-- 注意：prop与input绑定的值一定要一致，否则验证规则中的value会报undefined，因为value即为绑定的input输入值 -->
        <el-input
          v-model="loginForm.verifycode"
          class="loginform-input"
          placeholder="请输入验证码"
        />
      </el-form-item>
      <el-form-item class="loginform-item" style="height: 51.6px">
        <div @click="refreshCode">
          <s-identify :identify-code="identifyCode" />
          <el-button
            type="text"
            class="textbtn"
            @click="refreshCode"
          >看不清，换一张</el-button>
        </div>
      </el-form-item>

      <el-button
        :loading="loading"
        type="primary"
        style="width: 100%; margin-bottom: 30px"
        @click.native.prevent="handleLogin"
      >
        {{ $t("login.logIn") }}
      </el-button>

      <div style="position: relative">
        <div class="tips">
          <span>账号 : admin</span>
          <span>密码 : admin123</span>
        </div>
        

        <el-popover
          placement="top-start"
          title="忘记密码"
          width="200"
          trigger="hover"
          content="请联系账户管理员重置密码，重置后的密码为身份证后8位。"
        >
          <el-button slot="reference" class="thirdparty-button" style="margin-right: 120px" type="text">
            忘记密码
          </el-button>
        </el-popover>
      </div>
    </el-form>

 
  </div>
</template>

<script>
import LangSelect from '@/components/LangSelect'
import SocialSign from './components/SocialSignin'
import SIdentify from './components/SIdentify'


export default {
  name: 'Login',
  components: { LangSelect, SocialSign, SIdentify },
  data() {
  


    
    return {
      loginForm: {
        username: '',
        password: '',
        verifycode: ''
      },
      passwordType: 'password',
      capsTooltip: false,
      loading: false,
      avatarLoading: false, // 头像加载
      licenseLoading: false, // 执照加载
      redirect: undefined,
      otherQuery: {},
 loginRules: {
        // verifycode: [
        //   { required: true, trigger: 'blur', validator: validateVerifycode }
        // ]
      },
     
      teacher: {

      },
      collegeList: [

      ],

      identifyCodes: '1234567890',
      identifyCode: ''

    }
  },
  watch: {
    $route: {
      handler: function(route) {
        const query = route.query
        if (query) {
          this.redirect = query.redirect
          this.otherQuery = this.getOtherQuery(query)
        }
      },
      immediate: true
    }
  },
  created() {
  },
  mounted() {
    // 验证码初始化
    this.identifyCode = ''
    this.makeCode(this.identifyCodes, 4)

    if (this.loginForm.username === '') {
      this.$refs.username.focus()
    } else if (this.loginForm.password === '') {
      this.$refs.password.focus()
    }
  },
  destroyed() {
    // window.removeEventListener('storage', this.afterQRScan)
  },
  methods: {


    // 生成随机数
    randomNum(min, max) {
      return Math.floor(Math.random() * (max - min) + min)
    },
    // 切换验证码
    refreshCode() {
      this.identifyCode = ''
      this.makeCode(this.identifyCodes, 4)
    },
    // 生成四位随机验证码
    makeCode(o, l) {
      for (let i = 0; i < l; i++) {
        this.identifyCode += this.identifyCodes[
          this.randomNum(0, this.identifyCodes.length)
        ]
      }
      console.log(this.identifyCode)
    },
    checkCapslock(e) {
      const { key } = e
      this.capsTooltip = key && key.length === 1 && (key >= 'A' && key <= 'Z')
    },
    showPwd() {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
      this.$nextTick(() => {
        this.$refs.password.focus()
      })
    },
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true
          this.$store.dispatch('user/login', this.loginForm)
            .then(() => {
              this.$router.push({ path: '/', query: this.otherQuery })
              this.loading = false
            })
            .catch(() => {
              this.loading = false
            })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    getOtherQuery(query) {
      return Object.keys(query).reduce((acc, cur) => {
        if (cur !== 'redirect') {
          acc[cur] = query[cur]
        }
        return acc
      }, {})
    }

  }
}
</script>

<style lang="scss">
/* 修复input 背景不协调 和光标变色 */
/* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */

$bg: #fff;
$light_gray: rgb(1, 1, 12);
$cursor: rgba(219, 217, 217, 0.876);

@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  .login-container .el-input input {
    color: $cursor;
  }
}

/* reset element-ui css */
.login-container {

  .el-drawer {
    width: 85% !important;
    background: #0f2133;
    color: $cursor !important;

  }

  .el-input {
    display: inline-block;
    // height: 47px;
    width: 85%;

    input {
      background: transparent;
      width: 100%;
      border-top: 0px;
      border-left: 0px;
      border-right: 0px;
      margin-left: 5px;
      // padding-left: 5px;
      // border-bottom: 1px;
      -webkit-appearance: none;
      border-radius: 0px;
      // padding: 12px 5px 12px 15px;
      color: #409EFF;
      height: 47px;
      //caret-color: $cursor;

      &:-webkit-autofill {
        box-shadow: 0 0 0px 1000px $bg inset !important;
        -webkit-text-fill-color: $cursor !important;
      }
    }
  }

  .el-form-item {
    border: 1px solid rgba(255, 255, 255, 0.1);
    width: 220px;
    // margin-top: 50px;
    // background: rgba(0, 0, 0, 0.1);
    // border-radius: 5px;
    // color: #dddddd;
  }
  .el-form-item__content {
    display: block;
  }
  .el-input__suffix {
    margin-right: -36px;
  }
  .el-form-item__label {
    text-align: left;
    padding-left: 10px;
    color: #ddd;
    line-height: 45px;
  }
  .el-radio-group {
    margin-left: 15px;
  }

  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409eff;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }
  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }
}
</style>

<style lang="scss" scoped>
$bg: #2d3a4b;
$dark_gray: #889aa4;
$light_gray: #eee;

.login-container {
  min-height: 100%;
  width: 100%;
  background-color: $bg;
  overflow: hidden;

  .loginform-item {
    width: 100%;
  }

  .loginform-input {
    .input {
      width: 100% !important;
    }

    // width: 85%;
    // width: 100%;
    border-buttom: 2px solid #ddd;
  }

  .login-form {
    position: relative;
    width: 520px;
    max-width: 100%;
    padding: 160px 35px 0;
    margin: 0 auto;
    overflow: hidden;
  }

  .tips {
    font-size: 14px;
    color: rgb(14, 3, 3);
    margin-bottom: 10px;

    span {
      &:first-of-type {
        margin-right: 16px;
      }

    }
  }

  .svg-container {
    padding: 6px 5px 6px 15px;
    color: $dark_gray;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
  }

  .title-container {
    position: relative;

    .title {
      font-size: 26px;
      color: #409EFF;
      margin: 0px auto 40px auto;
      text-align: center;
      font-weight: bold;

    }

    .set-language {
      color: rgb(15, 15, 15);
      position: absolute;
      top: 3px;
      font-size: 18px;
      right: 0px;
      cursor: pointer;
    }
  }

  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }

  .thirdparty-button {
    position: absolute;
    right: 0;
    bottom: 6px;
  }

  .s-canvas{
    display: inline-block;
    height: 51.6px;
  }

  .textbtn {
    float: right;
    margin-right: 30px;
    line-height: 30px;
  }

  @media only screen and (max-width: 470px) {
    .thirdparty-button {
      display: none;
    }
  }
  background: #fff;


  .login-form{
    float: right;
    margin-right: 150px;
  }
}
</style>
